﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LiveProjectManager.DDD;


namespace LiveProjectManager.Controllers
{
    public class ResourceController : Controller
    {
        private static DDD.Services.ResourceService _service = new DDD.Services.ResourceService();
        private static DDD.Services.UserService _userService = new DDD.Services.UserService();
        private static DDD.Services.FileService _fileService = new DDD.Services.FileService();
        private static DDD.Services.CategoryService _categoryService = new DDD.Services.CategoryService();


        //
        // GET: /Resource
        public ActionResult Index(int paginateBy = 0, int page = 1)
        {
            return Json(
                DDD.Services.ResourceService.toDict(_service.getAll(paginateBy, page)),
                JsonRequestBehavior.AllowGet
            );
        }


        //
        // GET: /Resource/Details/{Id}
        public ActionResult Details(long id)
        {
            DDD.Domains.Resource myResource = _service.getById(id);

            // Can't find a matching resource.
            if (myResource == null)
                return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);

            // Return formatted resource.
            return Json(myResource.toDict(), JsonRequestBehavior.AllowGet);
        }


        //
        // DELETE: /Resource/Delete
        [HttpDelete]
        public ActionResult Delete(FormCollection myFormCollection)
        {
            try
            {
                // Entity to delete is not found.
                if (!_service.exists(long.Parse(myFormCollection["Id"])))
                    return new HttpStatusCodeResult((int)HttpStatusCode.NotFound);

                // Unknown error occured, unable to delete.
                if (!_service.remove(long.Parse(myFormCollection["Id"])))
                    return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);

                // Deletion successful
                return new HttpStatusCodeResult((int)HttpStatusCode.NoContent);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Invalid Id parameter (must be a long).");
            }
        }


        //
        // POST: /Resource/Create
        [HttpPost]
        public ActionResult Create(FormCollection myFormData)
        {
            try
            {
                // Parse & validate user.
                DDD.Domains.User myUser = _userService.getById(long.Parse(myFormData["UserId"]));
                if (myUser == null)
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "No User found with UserId: " + myFormData["UserId"] + ".");

                // Parse & validate category.
                DDD.Domains.Category myCategory = _categoryService.getById(long.Parse(myFormData["CategoryId"]));
                if (myCategory == null)
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "No Category found with CategoryId: " + myFormData["CategoryId"] + ".");

                // Parse & validate file.
                DDD.Domains.File myFile = _fileService.getById(long.Parse(myFormData["FileId"]));
                if (myFile == null)
                    return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "No File found with FileId: " + myFormData["FileId"] + ".");

                if (!_service.create(myFormData["Label"], myCategory, myUser, myFile, myFormData["Description"]))
                    return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
                return new HttpStatusCodeResult((int)HttpStatusCode.Created);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
            }
        }
    }
}

